import VueCoreImageUpload  from 'vue-core-image-upload'
import myComponent  from '../alert/alert.vue'

export default {
    components: {
        myComponent,
        'vue-core-image-upload': VueCoreImageUpload
    },
    data() {
        return {
            user: {
                name: '',
                age: '',
                pro: '',
                degree: '',
                tel: '',
                declaration: '',
                img: ''
            },
            imgload : false,
            prompt: false
        }
    },

    mounted() {
        window.scrollTo(0,0);
    },

    methods: {
        //添加上传图片
        imagechanged(res) {
            this.imgload = true;
        },
        //图片上传回调
        imageuploaded(res) {
            this.imgload = false;
            console.log(res)
            if(res.msg=='上传成功'){
                this.user.img = 'http://h5.oeeee.com/data/images/'+res.filesrc;
            }
        },
        // 提交报名信息
        submitSign() {
            if (!this.verity()) return;
            var postData = {
                name : this.user.name,
                age	: this.user.age,
                job	: this.user.pro,
                eyesituation : this.user.degree,
                mobile : this.user.tel,
                declaration: this.user.declaration,
                imageurl : this.user.img
            };
            // GET /someUrl
            this.$http.post('/index.php?s=/EyesDsj/joingame',postData).then(function(res){
                // console.log(res.data);
                // get body data
                if(res.data.errcode==0){
                    this.$refs.alerts.showalert(res.body.errmsg, '谢谢您的参与,我们将尽快审核');
                }else{
                    this.showAlert(res.data.errmsg);
                }
            });

        },
        // 校验
        verity(){
            if (!this.user.name) {
                this.showAlert('请输入您的姓名！');
                return false;
            } else if (!this.user.age) {
                this.showAlert('请输入您的年龄！');
                return false;
            } else if (!this.user.pro) {
                this.showAlert('请输入您的职业！');
                return false;
            } else if (!this.user.degree) {
                this.showAlert('请输入眼睛状况！');
                return false;
            } else if (!this.user.tel) {
                this.showAlert('请输入电话！');
                return false;
            } else if (!this.user.declaration) {
                this.showAlert('请输入参赛宣言！');
                return false;
            } else if (!this.user.img) {
                this.showAlert('请上传参赛图片！');
                return false;
            } else {
                return true;
            }
        },
        //显示错误弹出框
        showAlert(value){
            var that = this;
            this.prompt = value;
            setTimeout(function () {
                that.prompt = false;
            }, 3000);
        }
    }
}
